# coding=utf-8
"""
作者：董新强 
创建时间：
描述：
"""
import re
from os import path
from random import uniform

from db_models.raw_questions import RawQuestions
from framework.db_session import DbSession
from framework.utilities import to_string

question_pattern = re.compile('^\d+[．.、]')
options_pattern = re.compile('^[ABCD][．.、,，：:]?')
if __name__ == '__main__':
    fdir = 'E:\\Projects\\mxcat\\documents\\office'
    files = [('of1', 'of1a'), ('of2', 'of2a'), ('of3', 'of3a')]

    question_ls = []
    for dimidx, (qf, af) in enumerate(files):
        qfile = open(path.join(fdir, qf), mode='r', encoding='utf-8').readlines()
        afile = open(path.join(fdir, af), mode='r', encoding='utf-8').readlines()

        for idx, answer in enumerate(afile):
            title = qfile[5 * idx].strip('\n')
            a, b, c, d = qfile[5 * idx + 1].strip('\n'), qfile[5 * idx + 2].strip('\n'), qfile[5 * idx + 3].strip('\n'), qfile[5 * idx + 4].strip('\n')
            a, b, c, d = options_pattern.sub('', a), options_pattern.sub('', b), options_pattern.sub('', c), options_pattern.sub('', d)
            answer = answer.strip('\n')

            print(question_pattern.sub('', title), a, b, c, d, answer, answer.strip()[-1])

            rq = RawQuestions()
            rq.title = question_pattern.sub('', title)
            rq.options = to_string([{"id": "a", "name": a}, {"id": "b", "name": b}, {"id": "c", "name": c}, {"id": "d", "name": d}])
            rq.answer = answer.strip()[-1].lower()
            rq.dim = 'office' + str(dimidx + 1)
            rq.b = uniform(-1.5, 2.5)
            question_ls.append(rq)

    with DbSession.create() as db:
        db.bulk_save_objects(question_ls)
